
-- || Created by Kryptoholyx

dx_lib = inherit(Singleton)




local screen = {guiGetScreenSize()}	
	-- // Usefull
function isMouveOverPosition ( x, y, w, h)
	if isCursorShowing () then
		local mouse = {getCursorPosition()}
		local cursorx,cursory = mouse[1]*screen[1], mouse[2]*screen[2]
		
		if cursorx > x and cursorx < x + w and cursory > y and cursory < y + h then
			return true
		else
			return false
		end
	end	
end
function GetAbsoluteCursorPosition ()
        if isCursorShowing() then
                local cx,cy = getCursorPosition()
                return cx*screen[1],cy*screen[2]
        end
        return 0,0
end

	-- // addEvent´s to work with the GUI
		addEvent ('onDxClose'	, 	true )
		addEvent ('onDxClick'	, 	true )
		addEvent ('onDxOpen'	, 	true )
		addEvent ('onDxMove'    ,   true )
		
		
	function dx_lib:render()

		for k,v in next,self.elements do
			if v ~= self.activeElement then
				if v.only == false then
					dxDrawRectangle ( v.x,v.y,v.width,v.height, tocolor(0,0,0,100))
					dxDrawRectangle ( v.x,v.y,v.width,v.twidth, tocolor(150,150,150,100))
					for k,n in next,v.elements["buttons"] do
								dxDrawText ( n.title,  v.x + n.x +( n.w/2), v.y+v.twidth + n.y +(n.h/2), v.x + n.x+( n.w/2), v.y+v.twidth + n.y +(n.h/2), tocolor ( 255, 255, 255, 255 ),1,"ariel","center","center")
								dxDrawRectangle ( v.x + n.x, v.y+v.twidth + n.y, n.w, n.h, tocolor ( 0, 0, 0, 100 ))
					end
				end
			end
		end
		local v = self.activeElement
			if v then
				if v.only == false then
					dxDrawRectangle ( v.x,v.y,v.width,v.height, tocolor(0,0,0,150))
					dxDrawRectangle ( v.x,v.y,v.width,v.twidth, tocolor(150,150,150,150))
					dxDrawText ( "Active",  v.x+v.width/2,v.y+10,v.x+v.width/2,v.y+10, tocolor ( 255, 255, 255, 255 ),1,"ariel","center","center")
				end
				if v.elements then
					for k,n in next,v.elements["buttons"] do
						dxDrawText ( n.title,  v.x + n.x +( n.w/2), v.y+v.twidth + n.y +(n.h/2), v.x + n.x+( n.w/2), v.y+v.twidth + n.y +(n.h/2), tocolor ( 255, 255, 255, 255 ),1,"ariel","center","center")
						if isMouveOverPosition( v.x + n.x, v.y+v.twidth + n.y, n.w, n.h) then
							dxDrawRectangle ( v.x + n.x, v.y+v.twidth + n.y, n.w, n.h, tocolor ( 150, 15, 15, 150 ))
						else
							dxDrawRectangle ( v.x + n.x, v.y+v.twidth + n.y, n.w, n.h, tocolor ( 0, 0, 0, 150 ))	
						end
					end
				end
			end

	end
	
	function dx_lib:click(btn, std)
		if btn == "left" and std == "down" then
			
			local t = self.activeElement
			for k,v in next,self.elements do

					if isMouveOverPosition(v.x, v.y, v.width, v.twidth) then
						if not isMouveOverPosition(t.x, t.y, t.width, t.height) or t == v then 
							local cx,cy = GetAbsoluteCursorPosition()
								self.offX, self.offY = cx-v.x,cy-v.y
								self.clickedElement = k
								self.activeElement = v
									t.active = 0
									v.active = 1
						end
					end
			end
		elseif btn == "left" and std == "up" then
			self.clickedElement = nil
		end
		
			for k,v in next,self.elements do
				for k,b in next,v.elements["buttons"] do
					if isMouveOverPosition ( v.x + b.x, v.y+v.twidth + b.y, b.w, b.h ) then
						if self:getParent(b,"buttons").active == 1 or v["only"] == true then
							triggerEvent ( 'onDxClick', root, b.pointer, btn, std )
						end
					end
				end
			end
	end
	
	function dx_lib:move(btn, std)
		        if self.clickedElement ~= nil and getKeyState ( "mouse1" ) then
						local t = self.elements[self.clickedElement]
							if t.active == 0 then return end
							local cx,cy = GetAbsoluteCursorPosition()
							t.x, t.y = cx-self.offX,cy-self.offY
							
							triggerEvent ('onDxMove', root, t.pointer )
							
				else
					self.clickedElement = nil
                end
			
	end
	
	function dx_lib:window(t,x,y,w,h,twidth)
		self.elements[#self.elements+1] = {
		title = t,
		x = x,
		y = y,
		width = w,
		height = h,
		twidth = twidth,
		active = 0,
		elements = {},
		pointer = nil,
		only = {}
		
		
		}
		self.elements[#self.elements]["elements"]["buttons"] = {}
		
		
		outputChatBox(#self.elements)
		return self.elements[#self.elements]
	end
	
	function dx_lib:getParent(element,utable)
		for k,v in next,self.elements do
			for l,c in next,v.elements[utable] do
				if c == element then
					return v
				end 
			end
		
		end
	end
	function dx_lib:setOnlyMode(element,bool)
		element["only"] = bool
		return element["only"]
	end
	function dx_lib:setActive(element,bool)
		self.activeElement = element
		element.active = 1
		return element.active
	end
	
	
	function dx_lib:button(t,x,y,w,h,parent)
		m_Button_Array = parent["elements"]["buttons"]
		parent["elements"]["buttons"][#m_Button_Array+1] = {
		title = t,
		x = x,
		y = y,
		w = w,
		h = h,
		pointer = nil
		}

		
		
		--// create a custom pointer
		parent["elements"]["buttons"][#m_Button_Array].pointer = tostring(parent["elements"]["buttons"][#m_Button_Array])
		
		outputChatBox(parent["elements"]["buttons"][#m_Button_Array].pointer)
		return parent["elements"]["buttons"][#m_Button_Array]
	end

	
	
	function dx_lib:constructor()
		self.elements = {}
		self.clickedElement = nil
		
		self.activeElement = {x = 0,y = 0,width = 0,height = 0,twidth = 0}
		self.offX = 0
		self.offY = 0

		
		outputChatBox(#self.elements)
		addEventHandler ("onClientCursorMove", root, bind(self.move,self) )
		addEventHandler ("onClientClick", root, bind(self.click,self))
		addEventHandler("onClientRender",root,bind(self.render,self))
		
		
	end
dx = dx_lib:getSingleton()
 
 
test = dx:window("NOTHING",200,500,150,150,20)
bttts = dx:button("Example",15,50,50,25,test)
dx:setOnlyMode(test,false)
dx:setActive(test,1)

addEventHandler("onDxClick",root,function (pointer,btn,state)

		outputChatBox("Example button click ".. btn .." | "..state)

end)
	
bindKey ("m","down", function () 


	showCursor ( not isCursorShowing ()) 
end )


